package com.py.credit.service.impl;

import com.py.credit.common.Page;
import com.py.credit.common.ServiceException;
import com.py.credit.dao.FeedbackDao;
import com.py.credit.entity.*;
import com.py.credit.service.*;
import com.py.credit.utils.SQLBuilder;
import com.py.credit.utils.UuidUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.sql.Timestamp;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private FeedbackDao feedbackDao;

    @Autowired
    private PluginApiService pluginApiService;

    @Autowired
    private RemovalService removalService;

    @Autowired
    private LhjcRemoteBaseService lhjcRemoteBaseService;

    @Autowired
    private SyslogService syslogService;

    @Autowired
    private OfficeService officeService;

    @Override
    public List<Map<String, Object>> selectByConditionForPage(String deptId, String jclb, String type, List<String> showColumns, String[] additionalColumns, String condition, Page page) {
        if (CollectionUtils.isEmpty(showColumns)) {
            throw new ServiceException("至少选择一列");
        }
        List<String> conditions = SQLBuilder.createConditionSql(condition);

        return feedbackDao.selectByConditionForPage(deptId, jclb, type, showColumns, additionalColumns, conditions, page);
    }

    @Override
    public String getContent(Template template, String jclb, String ajbh, String ztmc) {
        List<String> list = new ArrayList<String>();
        Map<String, Object> map = new HashMap<String, Object>();

        String content = "";
        String content1 = "";
        String aString2 = "";
        if (template != null) {
            content = template.getContent();
            content1 = template.getContent();
            String rgex = "(?<=\\{\\{)(.*?)(?=\\}\\})";
            Pattern p = Pattern.compile(rgex);
            Matcher m = p.matcher(content);
            while (m.find()) {
                list.add(m.group());
            }
        }

        boolean flag;

        if (jclb.equals("C")) {
            jclb = "联合惩戒";
        } else {
            jclb = "联合激励";
        }

        map.put("事件编号", ajbh);
        map.put("主体名称", ztmc);
        map.put("事件类别", jclb);

        for (String in : map.keySet()) {
            //map.keySet()返回的是所有key的值
            Object str11 = new StringBuffer();
            str11 = map.get(in);//得到每个key多对用value的值
            String aString = "{{" + in + "}}";
            if (str11 == null) {
                aString2 = content1.replace(aString, "");
                content1 = aString2;
            } else {
                aString2 = content1.replace(aString, str11.toString());
                content1 = aString2;
            }

        }

        return content1;
    }

    @Override
    public void addFeedback(String ajbh, String feedbackTime, String feedbackMess, String cfbm, List<String> csIdList, SysUser userInfo, String token) {
        String batchId = UuidUtil.getInstance().getUUid();
        int feedbackId = pluginApiService.insertFeedbackInfo(userInfo.getId(), userInfo.getOfficeId(), ajbh, Timestamp.valueOf(feedbackTime), feedbackMess, batchId, userInfo.getRealName());

        for (int i = 0; i < csIdList.size(); i++) {
            pluginApiService.insertFeedbackCs(feedbackId, csIdList.get(i), batchId);
        }

        pluginApiService.modifyMess(Timestamp.valueOf(feedbackTime), ajbh);

        Removal rm = removalService.selectInfo(ajbh);

        //消息中心发送信息
        Integer mbId = null;

        if (rm.getJclb().equals("C")) {
            mbId = removalService.selectMbId("B");
        } else {
            mbId = removalService.selectMbId("J");
        }

        if (null != mbId) {
            List<MsgContentEntity> msgContentEntityList = new ArrayList<MsgContentEntity>();

            List<Long> topAndDownwardIdList = officeService.selectTopAndDownwardId(Long.parseLong(cfbm));
            List<Long> managerList = lhjcRemoteBaseService.getDeptManager(StringUtils.join(topAndDownwardIdList, ","), token);

            HashSet<Long> managerIdList = new HashSet<Long>();

            if (managerList != null && managerList.size() != 0) {
                managerIdList.addAll(managerList);
            }

            MsgContentEntity msgContentEntity = new MsgContentEntity();
            if (rm.getJclb().equals("C")) {
                msgContentEntity.setMsgTitle("联合惩戒反馈");
                msgContentEntity.setMsgTypeS("B");
            } else {
                msgContentEntity.setMsgTitle("联合激励反馈");
                msgContentEntity.setMsgTypeS("J");
            }

            msgContentEntity.setMsgContent(this.getContent(syslogService.selectMbInfo(mbId), rm.getJclb(), ajbh, rm.getZtmc()));
            msgContentEntity.setMsgType(4);
            msgContentEntity.setSendId(userInfo.getId());
            msgContentEntity.setSendName(userInfo.getRealName());

            List<MsgDetailEntity> msgDetailEntityList = new ArrayList<MsgDetailEntity>();

            for (Long managerId : managerIdList) {
                MsgDetailEntity msgDetailEntity = new MsgDetailEntity();
                msgDetailEntity.setReviever(managerId);
                msgDetailEntity.setRevieveType(1);
                msgDetailEntityList.add(msgDetailEntity);
            }

            msgContentEntity.setDetails(msgDetailEntityList);
            msgContentEntityList.add(msgContentEntity);

            lhjcRemoteBaseService.sendMsg(msgContentEntityList, token);
        }

        syslogService.addProcess(ajbh, "B" + rm.getJclb(), "Y", userInfo.getUsername(), userInfo.getRealName(), userInfo.getId(), userInfo.getOfficeId());

    }

	@Override
	public List<List<Map<String, String>>> getCreditviewData() {
		List<List<Map<String, String>>>  list = new ArrayList<List<Map<String,String>>>();
		List<Map<String, String>>  Honlist = new ArrayList<Map<String,String>>();
		List<Map<String, String>>  Heilist = new ArrayList<Map<String,String>>();
		//获取部门红名单数量
		Map<String, String> honData =  feedbackDao.getCreditviewhonData();
		//获取部门黑名单数量
		Map<String, String> heiData =  feedbackDao.getCreditviewheiData();
		Honlist.add(honData);
		Heilist.add(heiData);
		list.add(Honlist);
		list.add(Heilist);
		return list;
	}

	@Override
	public Map<String, String> getCreditviewLhjcData(String keyWord) {
		
		return feedbackDao.getCreditviewLhjcData(keyWord);
	}
}
